//引入experss模块
const express = require('express');
//引入连接池
const pool = require('../pool');
//创建产品路由器
const userCart = express.Router();
//创建用户添加购物的接口
userCart.post('/into_user_cart', (req, res) => {
  let pid = req.body.pid;
  let tprice = req.body.tprice;
  let num = req.body.num;
  let userid = req.body.userid;
  // console.log(userid)
  let sql = 'INSERT tykyo_user_cart(pid,tprice,num,userid) VALUES(?,?,?,?)';
  pool.query(sql, [pid,tprice,num,userid], (error, result) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200 ,result:result});
  })
});
//创建用户查询购物车的接口
userCart.get('/get_user_cart',(req,res)=>{
  let userid=req.query.userid;
  let pid=req.query.pid;
  let sql ='select pid,tprice,num from tykyo_user_cart where userid=?';
  pool.query(sql,[userid,pid],(error,result)=>{
    console.log(result)
    if (error) throw error;
    res.send({ message: 'ok', code: 200, result:result});
  })
})
//创建用户查询购物车详细信息的接口
userCart.post('/gett_user_cart',(req,res)=>{
  let userid = req.body.userid;
  let sql='(select productid as id,producttitle as title,productdesc as slogan,productphoto as photo,productprice as price from tykyo_user_cart left outer join tykyo_product on pid=productid where userid=?) union (select productid as id,producttitle as title,productdesc as slogan,productphoto as photo,productprice as price from tykyo_user_cart right outer join tykyo_product on pid=productid where userid=?)';
  pool.query(sql,[userid,userid],(err,result)=>{
    console.log(result);
    if(err) throw err;
    res.send({message: 'ok', code: 200, results:result})
  })
})
//用户删除某商品
userCart.post('/delete_user_cart',(req,res)=>{
  let userid = req.body.userid
  let pid = req.body.pid;
  let sql = 'delete from tykyo_user_cart where userid=? and pid=?'
  pool.query(sql,[userid,pid],(err,result)=>{
    if(err) throw err;
    res.send({message:'ok',code:200})
  })
})
module.exports=userCart;
